package com.example.demo.mapper;

import com.example.demo.entity.DTO.PetDTO;
import com.example.demo.entity.PetAndUser;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 宠物与主人中间表 Mapper 接口
 * </p>
 *
 * @author Mr.sdy
 * @since 2024-06-05
 */
@Mapper
public interface PetAndUserMapper extends BaseMapper<PetAndUser> {
    //添加
    @Insert("insert into pet_and_user(pet_id,user_id,create_time) values(#{petId},#{userId},now())")
    int insertPU(PetAndUser user);

    //通过userid查询宠物
    @Select("SELECT t2.pet_name,t2.pet_sex,t2.pet_age,t2.pet_img,t2.pet_info \n" +
            "FROM pet_and_user t1 \n" +
            "INNER JOIN pet t2 \n" +
            "on t1.pet_id = t2.pet_id \n" +
            "WHERE t1.user_id=#{userId}")
    List<PetDTO> selectAllPetsByUserId(Integer userId);

    //通过petid和userid查询中间表
    @Select("SELECT * FROM pet_and_user WHERE pet_id=#{petId} and user_id=#{userId}")
    List<PetAndUser> selectByPetIdAndUserId(PetAndUser petAndUser);
}
